# author = "Ronald"
# date   = 24-04-2010 07:29:02 PM

from OpenGL.GL import *
from OpenGL.GL import glScale
from OpenGL.GLU import *
from OpenGL.GLUT import *
from numpy import *

# proporciones del largo de las partes de la pierna
THIGH = 2.0
CRUS = 2.5
KNEE = 0.1
HIP = 1.0

# proporci'on para hacer el brazo a escala
#SIZE = 0.5


class Leg(object):

    def __init__(self, size, position):
        self.size = size
        self.position = position
        self.thighY = 90
        self.thighZ = 90
        self.crusY = 0
        self.crusZ = 0


    def draw(self):

        # dibujar la cadera
        glColor3f (0.0, 0.0, 1.0)
        glPushMatrix()
        glScalef(self.size, self.size, self.size)
        glTranslatef (self.position[0], self.position[1], self.position[2])
        #glTranslatef (-ARM/2, 0.0, 0.0)
        glTranslatef(-HIP/2, 0.0, 0.0)
        glRotatef(self.thighZ, 0.0, 1.0, 0.0)
        glRotatef(self.thighY, 0.0, 0.0, 1.0)
        glutSolidSphere(HIP/2, 10, 10)      # self.thighY
        glPopMatrix()

        # dibujar el muslo
        glColor3f (1.0, 1.0, 1.0)
        glPushMatrix()
        glScalef(self.size, self.size, self.size)
        glTranslatef (self.position[0], self.position[1], self.position[2])
        #glTranslatef (-ARM/2, 0.0, 0.0)
        glTranslatef (-HIP/2, 0.0, 0.0)
        glRotatef(self.thighZ, 0.0, 1.0, 0.0)
        glRotatef(self.thighY, 0.0, 0.0, 1.0)
        glTranslatef (-(THIGH/2 + HIP/2), 0.0, 0.0)
        glScalef(THIGH, 1, 1)
        glutSolidCube(1)      # self.thighY
        glPopMatrix()

        # dibujar la rodilla
        glColor3f (0.0, 0.0, 0.5)
        glPushMatrix()
        glScalef(self.size, self.size, self.size)
        glTranslatef (self.position[0], self.position[1], self.position[2])
        #glTranslatef (-ARM/2, 0.0, 0.0)
        glTranslatef (-HIP/2, 0.0, 0.0)
        glRotatef(self.thighZ, 0.0, 1.0, 0.0)
        glRotatef(self.thighY, 0.0, 0.0, 1.0)
        glTranslatef(-(KNEE/2 + THIGH + HIP/2), 0.0, 0.0)
        glutSolidSphere(KNEE/2, 10, 10)      # self.thighY
        glPopMatrix()

        # dibujar la pierna (pantorrilla)
        glColor3f (0.0, 0.0, 1.0)
        glPushMatrix()
        glScalef(self.size, self.size, self.size)
        glTranslatef (self.position[0], self.position[1], self.position[2])
        #glTranslatef (-1.0, 0.0, 0.0)
        glTranslatef (-HIP/2, 0.0, 0.0)
        glRotatef(self.thighZ, 0.0, 1.0, 0.0)
        glRotatef(self.thighY, 0.0, 0.0, 1.0)
        glTranslatef (-(KNEE/2 + THIGH + HIP/2), 0.0, 0.0)
        glRotatef (self.crusY, 0.0, 0.0, 1.0)
        glTranslatef (-(CRUS/2 + KNEE/2), 0.0, 0.0)
        glScalef(CRUS, 1, 1)
        glutSolidCube(1.5)      # self.crusY
        glPopMatrix()

        #mover el brazo a la posicion correspondiente
        glPushMatrix()
        glTranslatef (self.position[0], self.position[1], self.position[2])
        glPopMatrix()




    #funciones de movimiento del brazo
    # mueve el codo hacia arriba
    def crusYAdd(self,speed):
        if self.crusY < 90:
            self.crusY = (self.crusY + speed) % 360

    # mueve el codo hacia abajo
    def crusYSubstract(self,speed):
        if self.crusY > 0:
            self.crusY = (self.crusY - speed) % 360

    # mueve el hombro hacia abajo
    def thighYAdd(self,speed):
        if self.thighY < 180:
            self.thighY = (self.thighY + speed) % 360

    # mueve el hombro hacia arriba
    def thighYSubstract(self,speed):
        if self.thighY > 0:
            self.thighY = (self.thighY - speed) % 360

    # mueve el hombro hacia delante
    def thighZAdd(self,speed):
        if self.thighZ < 90:
            self.thighZ = (self.thighZ + speed) % 360

    def thighZSubstract(self,speed):
        if self.thighZ > 0:
            self.thighZ = (self.thighZ - speed) % 360

    def runFront(self,speed):
#        self.crusYAdd()
        self.thighYSubstract(speed)

    def runFront2(self,speed):
        self.crusYSubstract(speed)

    def runFront3(self,speed):
        self.thighYAdd(speed)
        self.crusYSubstract(speed)

    def runBack(self,speed):
        self.thighYAdd(speed)

    def runBack2(self,speed):
        self.crusYAdd(speed)

    def runBack3(self,speed):
        self.crusYAdd(speed)
        self.thighYSubstract(speed)
